package cn.tedu.appointment.work.service.impl;

import cn.tedu.appointment.accounts.mapper.StaffMapper;
import cn.tedu.appointment.accounts.pojo.dto.StaffSearchParam;
import cn.tedu.appointment.accounts.pojo.vo.StaffVO;
import cn.tedu.appointment.base.response.JsonResult;
import cn.tedu.appointment.work.mapper.ApplicationMapper;
import cn.tedu.appointment.work.pojo.dto.ApplicationParam;
import cn.tedu.appointment.work.pojo.dto.ApplicationQuery;
import cn.tedu.appointment.work.pojo.entity.Application;
import cn.tedu.appointment.work.pojo.vo.ApplicationVO;
import cn.tedu.appointment.work.service.ApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    StaffMapper staffMapper;


    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("搜索请假业务:applicationQuery={}", applicationQuery);
        StaffSearchParam staffSearchParam = new StaffSearchParam();
        List<ApplicationVO> applicationVOList = new ArrayList<>();
        if (applicationQuery.getId() != null) {
            log.debug("查询详情");
            applicationVOList.addAll(applicationMapper.select(applicationQuery));
        } else {
            if (!applicationQuery.getStaffName().isEmpty()) {
                log.debug("通过员工姓名查询");
                staffSearchParam.setName(applicationQuery.getStaffName());
                List<StaffVO> staffVOS = staffMapper.select(staffSearchParam);
                for (int i = 0; i < staffVOS.size(); i++) {
                    applicationQuery.setStaffId(staffVOS.get(i).getId());
                    applicationVOList.addAll(applicationMapper.select(applicationQuery));
                }
            }
            if ("2".equals(applicationQuery.getJob()) || "3".equals(applicationQuery.getJob())) {
                log.debug("通过员工Id查询");
                applicationVOList.addAll(applicationMapper.select(applicationQuery));
            } else {
                log.debug("通过管理员查询");
                applicationQuery.setStaffId(null);
                applicationVOList.addAll(applicationMapper.select(applicationQuery));
                log.debug("查询订单：{}", applicationVOList.toString());
            }
        }
        for (int i = 0; i < applicationVOList.size(); i++) {
            Long staffId = applicationVOList.get(i).getStaffId();
            staffSearchParam.setId(staffId);
            List<StaffVO> staffVOList = staffMapper.select(staffSearchParam);
            applicationVOList.get(i).setStaffVO(staffVOList.get(0));
            log.debug("查询员工：{}", staffVOList.get(0).getName());
        }
        for (int i = 0; i < applicationVOList.size(); i++) {
            if ("0".equals(applicationQuery.getJob()) || "1".equals(applicationQuery.getJob())) {
                if ("20".equals(applicationVOList.get(i).getStatus())) {
                    applicationVOList.remove(i);
                }
            }
        }
        log.debug(applicationVOList.toString());
        return applicationVOList;
    }

    @Override
    public void updateStatus(ApplicationParam applicationParam) {
        log.debug("驳回请假申请业务:applicationParam={}", applicationParam);
        Application application = new Application();
        if (applicationParam.getId() == null) {
            // 添加请假申请
        } else {
            if (applicationParam.getRejectReason().isEmpty()) {
                application.setId(applicationParam.getId());
                application.setStatus("40");
                application.setUpdateTime(new Date());
                applicationMapper.update(application);
            } else {
                BeanUtils.copyProperties(applicationParam,application);
                application.setStatus("30");
                application.setUpdateTime(new Date());
                applicationMapper.update(application);
            }
        }
    }

    @Override
    public void deleteApplication(Long id) {
        log.debug("删除请假申请业务:id={}",id);
        applicationMapper.delete(id);
    }
}
